package com.huichenghe.xinlvsh01.CustomView;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huichenghe.xinlvsh01.Adapter.RecyclerViewAdapter;
import com.huichenghe.xinlvsh01.Ble.LocalDeviceEntity;
import com.huichenghe.xinlvsh01.CommonUtilss.CommonUtils;
import com.huichenghe.xinlvsh01.DataEntites.OutLineDataEntity;
import com.huichenghe.xinlvsh01.DataEntites.chooseTypeEntity;
import com.huichenghe.xinlvsh01.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 离线运动选择Dialog
 * Created by lixiaoning on 15-12-1.
 */
public class CustomChooseTypeDialog
{
    public static final String TAG = CustomChooseTypeDialog.class.getSimpleName();
    private MYAdapter myAdapter;
    Dialog dialog;
    private ArrayList<chooseTypeEntity> dataList;
    private Context mContext;
    private ChooseDialogCallback mChooseDialogCallback;

    public interface ChooseDialogCallback
    {
        public void onChoose(int Type, String sportName);
    }

    public CustomChooseTypeDialog(Context mContext, ChooseDialogCallback callback)
    {
        this.mContext = mContext;
        this.mChooseDialogCallback = callback;

        // 数据
        dataList = new ArrayList<chooseTypeEntity>();
        dataList.add(new chooseTypeEntity(R.drawable.walk_icon, R.string.walk, OutLineDataEntity.TYPE_WALK));
        dataList.add(new chooseTypeEntity(R.drawable.running, R.string.running, OutLineDataEntity.TYPE_RUNNINT));
        dataList.add(new chooseTypeEntity(R.drawable.climing, R.string.climing, OutLineDataEntity.TYPE_CLIMING));
        dataList.add(new chooseTypeEntity(R.drawable.ball, R.string.ball, OutLineDataEntity.TYPE_BALL_MOVEMENT));
        dataList.add(new chooseTypeEntity(R.drawable.muscle, R.string.muscle, OutLineDataEntity.TYPE_MUSCLE));
        dataList.add(new chooseTypeEntity(R.drawable.aerobic, R.string.aerobic, OutLineDataEntity.TYPE_AEROBIC));

        dialog = new Dialog(mContext);
        LayoutInflater inflater =
                (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.choose_type_layout, null);
        dialog.setContentView(linearLayout, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        RecyclerView recyclerView = (RecyclerView)linearLayout.findViewById(R.id.layout_choose);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(new MYAdapter());

        settingTheDialogWidthAndHeight(recyclerView);



        dialog.setCanceledOnTouchOutside(true);
        dialog.show();



    }


    /**
     * 根据屏幕，设置dialog的宽高
     */
    private void settingTheDialogWidthAndHeight(RecyclerView view)
    {
        BitmapFactory.Options options = new BitmapFactory.Options();    // 对图片进行处理
        options.inJustDecodeBounds = true;                              // 只获取图片的边界属性
        BitmapFactory.decodeResource(mContext.getResources(),  R.drawable.walk, options );// 解析图片

        LinearLayout.LayoutParams listLp =   (LinearLayout.LayoutParams) view.getLayoutParams();
        listLp.width = (int)(getSceenWidthPixels(mContext) * 0.8f);
        listLp.height = (int) ((options.outHeight + CommonUtils.Dp2Px(mContext, 23)) * dataList.size()) ;
        view.setLayoutParams(listLp);




    }


    private int getSceenWidthPixels(Context mContext)
    {
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        return dm.widthPixels;
    }

    /**
     * dialog的Adapter
     */
    class MYAdapter extends RecyclerView.Adapter<MYAdapter.MyHolder>
    {


        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType)
        {
            View v = LayoutInflater.from(mContext).inflate(R.layout.movement_type_choose, parent, false);
            MyHolder holder = new MyHolder(v);
            return holder;
        }

        @Override
        public void onBindViewHolder(final MyHolder holder, final int position)
        {
            holder.mImageView.setImageResource(dataList.get(position).getIconId());
            holder.mTextView.setText(dataList.get(position).getTypeName());
            holder.mCardView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    mChooseDialogCallback.onChoose(
                    dataList.get(position).getType(), mContext.getString(dataList.get(position).getTypeName()));
                    dialog.dismiss();
                }
            });
        }



        @Override
        public int getItemCount()
        {
            return dataList.size();
        }

        /**
         * viewholder
         */
        class MyHolder extends RecyclerView.ViewHolder
        {
            ImageView mImageView;
            TextView mTextView;
            CardView mCardView;

            public MyHolder(View itemView)
            {
                super(itemView);
                mImageView = (ImageView)itemView.findViewById(R.id.type_icon);
                mTextView = (TextView)itemView.findViewById(R.id.type_name);
                mCardView = (CardView)itemView.findViewById(R.id.choose_movement_type_cardview);



            }
        }
    }
}
